package yunwo.cn.yzstation.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.BasicAdapter;
import yunwo.cn.yzstation.bean.CinfoBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.view.activity.CarOrderDetailsActivity;
import yunwo.cn.yzstation.view.activity.OrderDetailsActivity;

/**
 * Created by lao on 2017/10/14.
 */

public class TrainnumberAdapter extends BasicAdapter<CinfoBean.DataBean.ListBean> {
    public TrainnumberAdapter(Context mctx) {
        super(mctx);
    }

    @Override
    protected int setItemLayout() {
        return R.layout.lvitem_trainnumber;
    }

    @Override
    protected void bindData(View convertView, final CinfoBean.DataBean.ListBean data, int position) {
        TextView batch = get(convertView, R.id.batch);
        final TextView arrive = get(convertView, R.id.arrive);
        TextView beginOrganName = get(convertView, R.id.beginOrganName);
        TextView carnumberpname = get(convertView, R.id.carnumberpname);
        TextView driverPhone = get(convertView, R.id.driverPhone);
        TextView factGoodNumber = get(convertView, R.id.factGoodNumber);
        TextView goodsNumber = get(convertView, R.id.goodsNumber);
        TextView weight = get(convertView, R.id.weight);

        switch (data.getStatus()) {
            case 2:
                arrive.setText("到车");
                break;
            case 3:
                arrive.setText("到货");
                break;
            case 4:
                arrive.setText("到货");
                break;
            case 5:
                arrive.setVisibility(View.GONE);
                break;
        }

        batch.setText(data.getBatchNumber());
        beginOrganName.setText(data.getBeginOrganName());
        carnumberpname.setText(data.getPlateNumber() + "(" + data.getDriverName() + ")");
        driverPhone.setText(data.getDriverPhone());
        factGoodNumber.setText(data.getFactGoodNumber() + "票");
        goodsNumber.setText(data.getGoodsNumber() + "件");
        weight.setText(data.getWeight() + "kg");


        arrive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (arrive.getText().toString().equals("到车")) {
                    showdalog(data, arrive);
                } else {
                    Intent intent = new Intent(mctx, CarOrderDetailsActivity.class);
                    intent.putExtra("id", data.getDepartId());
                    mctx.startActivity(intent);
                }


            }
        });
    }

    private void showdalog(final CinfoBean.DataBean.ListBean data, final TextView arrive) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mctx);
        builder.setCancelable(false);
        builder.setTitle("确定到车？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, final int i) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", data.getDepartId());
                HttpManager.hpptPostString3(MyConstants.Asterm, map).execute(new GLCallBack() {
                    @Override
                    public void onResponseT(String result, int code, String msg, int id) {
                        if (code == 200) {
                            arrive.setText("到货");
                        } else {
                            Toast.makeText(mctx, msg, Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(mctx, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        builder.show();
    }
}
